package com.hbnu.mysql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class DatabaseDemo4 {
    public static void main(String[] args) {
        Connection connection = null;
        Statement statement = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");          //通过反射创建连接

            //获取数据库连接
            String url = "jdbc:mysql://localhost:3306/class2211?serverTimezone=UTC&useSSL=false&characterEncoding=utf-8";
            String user = "root";
            String password = "12123";
            connection = DriverManager.getConnection(url,user,password);

            //获取数据库操作对象
            statement = connection.createStatement();

            //执行SQL
            String sql = "DELETE FROM nncc1 WHERE c1 = 'John'";             //这里如果是字符型数据，要额外加入引号
            int row = statement.executeUpdate(sql);
            System.out.println("影响了数据库表"+row+"条数据");





        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        finally {
            //关闭数据库资源
            try {
                if (statement != null){
                    statement.close();
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            try {
                if (connection != null){
                    connection.close();
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
